﻿using AutoMapper;
using Pay.Core;
using Pay.Data;
using Pay.DTO;
using Pay.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Pay.Service.AutoMapper {

    public class TokenTransferMapper : IStartupDto {
        private IMapperConfigurationExpression _cfg;
        public TokenTransferMapper(IMapperConfigurationExpression cfg) {
            _cfg = cfg;
        }
        public void Execute() {
            Create();
        }
        protected virtual void Create() {

            _cfg.CreateMap<TokenTransfer, token_transfer_dto>()
                .ForMember(dest => dest.id, src => src.MapFrom(c => c.Id))
                .ForMember(dest => dest.merchant_id, src => src.MapFrom(c => c.MerchantId))
                .ForMember(dest => dest.merchant_name, src => src.MapFrom(c => c.Merchant.Name))
                .ForMember(dest => dest.merchant_mobile, src => src.MapFrom(c => c.Merchant.Mobile))
                .ForMember(dest => dest.audit_Manage, src => src.MapFrom(c => c.AuditManage.Mobile))
                .ForMember(dest => dest.pay_manage, src => src.MapFrom(c => c.PayManage.Mobile))
                .ForMember(dest => dest.order_no, src => src.MapFrom(c => c.OrderNo))
                .ForMember(dest => dest.transfer_amount, src => src.MapFrom(c => c.TransferAmount))
                .ForMember(dest => dest.amount, src => src.MapFrom(c => c.Amount))
                .ForMember(dest => dest.status, src => src.MapFrom(c => c.Status))
                .ForMember(dest => dest.status_desc, src => src.MapFrom(c => c.Status.ToDescription()))
                .ForMember(dest => dest.transfer_type, src => src.MapFrom(c => c.TransferType))
                .ForMember(dest => dest.transfer_type_desc, src => src.MapFrom(c => c.TransferType.ToDescription()))
                .ForMember(dest => dest.settle_amount, src => src.MapFrom(c => c.SettleAmount))
                .ForMember(dest => dest.settle_rate, src => src.MapFrom(c => c.SettleRate))
                .ForMember(dest => dest.region_name, src => src.MapFrom(c => c.RegionName))
                .ForMember(dest => dest.bank_name, src => src.MapFrom(c => c.BankName))
                .ForMember(dest => dest.account_no, src => src.MapFrom(c => c.AccountNo))
                .ForMember(dest => dest.account_name, src => src.MapFrom(c => c.AccountName))
                .ForMember(dest => dest.branch_address, src => src.MapFrom(c => c.BranchAddress))
                .ForMember(dest => dest.busi_type, src => src.MapFrom(c => c.BusiType))
                .ForMember(dest => dest.pay_id, src => src.MapFrom(c => c.PayId))
                .ForMember(dest => dest.pay_info, src => src.MapFrom(c => c.PayInfo))
                .ForMember(dest => dest.remark, src => src.MapFrom(c => c.Remark))
                .ForMember(dest => dest.is_risk, src => src.MapFrom(c => c.IsRisk))
                .ForMember(dest => dest.apply_time, src => src.MapFrom(c => c.ApplyTime.ToDateTime()))
                .ForMember(dest => dest.audit_time, src => src.MapFrom(c => c.AuditTime.ToDateTime()))
                .ForMember(dest => dest.pay_time, src => src.MapFrom(c => c.PayTime.ToDateTime()));
            _cfg.CreateMap<token_transfer_dto, TokenTransfer>()
                .ForMember(dest => dest.MerchantId, src => src.MapFrom(c => c.merchant_id))
                .ForMember(dest => dest.OrderNo, src => src.MapFrom(c => c.order_no))
                .ForMember(dest => dest.TransferAmount, src => src.MapFrom(c => c.transfer_amount))
                .ForMember(dest => dest.TransferType, src => src.MapFrom(c => c.transfer_type))
                .ForMember(dest => dest.SettleAmount, src => src.MapFrom(c => c.settle_amount))
                .ForMember(dest => dest.PayId, src => src.MapFrom(c => c.pay_id))
                .ForMember(dest => dest.RegionName, src => src.MapFrom(c => c.region_name))
                .ForMember(dest => dest.BankName, src => src.MapFrom(c => c.bank_name))
                .ForMember(dest => dest.AccountName, src => src.MapFrom(c => c.account_name))
                .ForMember(dest => dest.AccountNo, src => src.MapFrom(c => c.account_no))
                .ForMember(dest => dest.BranchAddress, src => src.MapFrom(c => c.branch_address))
                .ForMember(dest => dest.BusiType, src => src.MapFrom(c => c.busi_type))
                .ForMember(dest => dest.IsRisk, src => src.MapFrom(c => c.is_risk))
                .ForMember(dest => dest.ApplyTime, src => src.MapFrom(c => c.apply_time.ToUnix()));

            _cfg.CreateMap<TokenTransfer, token_wallet_bill_detail_dto>()
                .ForMember(dest => dest.nick_name, src => src.MapFrom(c => c.Merchant.Name))
                .ForMember(dest => dest.mobile, src => src.MapFrom(c => c.Merchant.Mobile))
                .ForMember(dest => dest.money, src => src.MapFrom(c => c.TransferAmount))
                .ForMember(dest => dest.order_no, src => src.MapFrom(c => c.OrderNo))
                .ForMember(dest => dest.datetime, src => src.MapFrom(c => c.ApplyTime.ToDateTime().ToString("yyyy-MM-dd HH:mm:ss")))
                .AfterMap((src, dest) => {
                    dest.status = (src.Status == TO.TokenTransferStatus.Passedpaid ? 1 : 0);
                    dest.status_desc = (dest.status == 2 ? "已结算" : "未结算");
                });
            _cfg.CreateMap<TokenTransfer, token_deposited_dto>()
                .ForMember(dest => dest.amount, src => src.MapFrom(c => c.TransferAmount.ToString("f2")))
                .ForMember(dest => dest.status, src => src.MapFrom(c => c.Status))
                .ForMember(dest => dest.datetime, src => src.MapFrom(c => c.ApplyTime.ToDateTime().ToString("yyyy-MM-dd HH:mm:ss")))
                .AfterMap((src, dest) => {
                    if (src.Status == TO.TokenTransferStatus.Fail)
                        dest.status_desc = src.Status.ToDescription();
                    else if (src.Status == TO.TokenTransferStatus.Auditing)
                        dest.status_desc = "审核中";
                    else if (src.Status == TO.TokenTransferStatus.Passedpaid)
                        dest.status_desc = "提现成功";
                    else if (src.Status == TO.TokenTransferStatus.Process || src.Status == TO.TokenTransferStatus.PayOffFail ||
                    src.Status == TO.TokenTransferStatus.PassedUnpaid)
                        dest.status_desc = "打款中";
                });
        }
    }
}
